//
//  goodsViewController.m
//  lmq
//
//  Created by 千翼网络科技 on 2017/1/13.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "goodsViewController.h"
#import "goodsCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "detailsViewController.h"
#import "configddd.h"

@interface goodsViewController ()

@end

@implementation goodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setTitle:@""];
    self.title = _tittleStr;
    self.view.backgroundColor=[ZJCModel colorWithString:@"f2f2f2"];
    
    [self requestData];
    
    UIButton *rightButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, (22), (22))];
    [rightButton addTarget:self action:@selector(rightNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageNamed:@"search"] forState:UIControlStateSelected];
    UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem=rightBarButton;

//    Collection
    
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = screenScale(20);//上下行cell的间距
    
    goodsView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-(1))];//493
    goodsView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:goodsView];
    
    goodsCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake((DEF_SCREEN_WIDTH-2*screenScale(288))/3, 0, DEF_SCREEN_WIDTH-screenScale(40), DEF_SCREEN_HEIGHT-64-49-(1)) collectionViewLayout:layout];
    goodsCollectionView.delegate=self;
    goodsCollectionView.dataSource=self;
    goodsCollectionView.alwaysBounceVertical = YES;
    goodsCollectionView.showsVerticalScrollIndicator = NO;
    goodsCollectionView.backgroundColor= [UIColor whiteColor];
    [goodsCollectionView registerClass:[goodsCollectionViewCell class] forCellWithReuseIdentifier:@"goodsCollectionViewCell"];
    [goodsView addSubview:goodsCollectionView];
    
    UIView *xView=[[UIView alloc]initWithFrame:CGRectMake(0, DEF_SCREEN_HEIGHT-49-64-(1), DEF_SCREEN_WIDTH, (1))];
    xView.backgroundColor=[UIColor grayColor];
    [self.view addSubview:xView];
}

- (void)requestData{

    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    [productParams setValue: self.myStr forKey:@"sid"];
    [[MNetworkHandler sharedInstance]conURL:Category_Goods
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       //[PublicFunction saveConfig:userTextField.text forKey:@"phone"];
                                       //[PublicFunction saveConfig:[returnData objectForKey:@"sessionID"] forKey:@"sessionID"];
                                       
                                       //                                       NSLog(@"===returnData===%@",returnData);
                                       goodsArr = [returnData objectForKey:@"data"];
                                       [goodsCollectionView reloadData];
                                       [hud removeFromSuperview];
                                   }
                                   else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [hud removeFromSuperview];
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
}

#pragma mark - 点击右导航
-(void)rightNavItemClick{
    NSLog(@"搜索");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - CollectionView ---------------------------------------

//section 的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

//cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return goodsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identify = @"goodsCollectionViewCell";
    goodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    NSDictionary *dict=goodsArr[indexPath.row];
    NSString *urlGood=[dict objectForKey:@"shoppic"];
    NSString *urlN=manage(AP_HOST, urlGood);
    NSString *strUrl = [urlN stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    NSURL *urlW=[NSURL URLWithString:strUrl];
    [cell.imageshow sd_setImageWithURL:urlW placeholderImage:[UIImage imageNamed:@"niunai"]];
    cell.myLabel.text=[dict objectForKey:@"shop_name"];
    cell.priceLabel.text=[dict objectForKey:@"price"];
    
    return cell;
}

-(void)itemSelect:(NSIndexPath*)index{

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",@(indexPath.row).description);
    
    detailsViewController *detailsVC=[[detailsViewController alloc]init];
    detailsVC.hidesBottomBarWhenPushed=YES;
    
    NSDictionary *aDict=goodsArr[indexPath.row];
    detailsVC.myStr=[aDict objectForKey:@"shop_id"];
    
    [self.navigationController pushViewController:detailsVC animated:YES];
}

#pragma mark -- UICollectionViewDelegate

//设置每个 UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(screenScale(288), screenScale(390));
}

//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    //    if (collectionView==hotCollectionView) {
    //
    //    }
    return UIEdgeInsetsMake((15), 0, 15,0);//上、左、下、右
}

//定义每个UICollectionView 的横向间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}


-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

@end
